package kunsch.bexp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import kunsch.bexp.BooleanFunction;
import kunsch.bexp.Expression;

public class BExpUtil
{

	public static Expression copyExpression(Expression expression)
	{
		serializeBooleanExpression(expression);

		return deserializeBooleanExpression();
	}

	public static Expression deserializeBooleanExpression()
	{
		try
		{
			File file = new File("tmp/tmp.tmp");

			FileInputStream fileIn = new FileInputStream(file);

			ObjectInputStream in = new ObjectInputStream(fileIn);
			Expression function = (Expression) in.readObject();
			in.close();
			fileIn.close();
			return function;
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private static void serializeBooleanExpression(Expression function)
	{
		try
		{
			File file = new File("tmp");
			file.mkdir();
			
			File newFile = new File("tmp/tmp.tmp");
			
			FileOutputStream fileOut = new FileOutputStream(newFile);
			ObjectOutputStream out;
			out = new ObjectOutputStream(fileOut);
			out.writeObject(function);
			out.close();
			fileOut.close();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
